package com.ext.aic.box;

import android.content.Context;

import com.ext.aic.AICApplication;
import com.ext.aic.AICManager;
import com.ext.aic.beans.HostingTimeItem;
import com.ext.aic.beans.HostingTimeItem_;
import com.ext.aic.utils.TimeUtils;
import com.ext.aic.utils.device.DeviceUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.util.List;

import io.objectbox.query.QueryBuilder;

public class ReplyHostingTask {
    public static boolean isAllowReply() {
        List<HostingTimeItem> timeSlotList = getTimeSlotList();
        if (ObjectJudge.isNullOrEmpty(timeSlotList)) {
            //未配置时默认允许回复
            return true;
        }
        for (HostingTimeItem hostingTimeItem : timeSlotList) {
            if (hostingTimeItem.isAdd()) {
                continue;
            }
            int startHour = hostingTimeItem.getStartHour();
            int startMinute = hostingTimeItem.getStartMinute();
            int endHour = hostingTimeItem.getEndHour();
            int endMinute = hostingTimeItem.getEndMinute();
            long startTime = TimeUtils.toTimestampByHM(startHour, startMinute);
            long endTime = TimeUtils.toTimestampByHM(endHour, endMinute);
            long timeMillis = System.currentTimeMillis();
            if (timeMillis >= startTime && timeMillis <= endTime) {
                return true;
            }
        }
        return false;
    }

    public static List<HostingTimeItem> getTimeSlotList() {
        return AICManager.getInstance().findList(HostingTimeItem.class, queryBuilder -> {
            Context applicationContext = AICApplication.getInstance().getApplicationContext();
            queryBuilder.equal(HostingTimeItem_.deviceId, DeviceUtils.getInstance().getDeviceId(applicationContext), QueryBuilder.StringOrder.CASE_INSENSITIVE);
        });
    }
}
